import { Controller, Post, Body } from "@nestjs/common";
import { UserAddDto, UserQueryDto, UserRemoveDto, UserUpdateDto } from "src/dto/user";
import { UserService } from "../service/user.service";
import { ApiRes, RequsetUser, UsePermGuard, definePermission } from "src/decorator";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { UserEntity } from "src/entity";

export const Perm = definePermission("user", {
    query: {
        name: "查询",
        code: "query",
        description: "用户查询权限",
    },
    add: {
        name: "添加",
        code: "add",
        description: "用户添加权限",
    },
    update: {
        name: "更新",
        code: "update",
        description: "用户更新权限",
    },
    remove: {
        name: "移除",
        code: "remove",
        description: "用户移除权限",
    },
} as const);

@ApiTags("User - 模块", "Admin - 模块")
@Controller("user")
export class AdminController {
    constructor(private readonly userService: UserService) {}

    @ApiOperation({ summary: "用户列表" })
    @ApiRes({ type: UserEntity, pagination: true })
    @UsePermGuard(Perm.QUERY)
    @Post("query")
    query(@Body() dto: UserQueryDto) {
        return this.userService.queryUser(dto);
    }

    @ApiOperation({ summary: "新增用户" })
    @ApiRes({ type: UserEntity })
    @UsePermGuard(Perm.ADD)
    @Post("add")
    addUser(@Body() dto: UserAddDto) {
        return this.userService.create(dto);
    }

    @ApiOperation({ summary: "修改用户" })
    @ApiRes({ type: UserEntity })
    @UsePermGuard(Perm.UPDATE)
    @Post("update")
    updateUser(@RequsetUser() user: RequsetUser, @Body() dto: UserUpdateDto) {
        return this.userService.updateUser(user, dto);
    }

    @ApiOperation({ summary: "移除用户" })
    @UsePermGuard(Perm.REMOVE)
    @Post("remove")
    removeUser(@RequsetUser() user: RequsetUser, @Body() dto: UserRemoveDto) {
        return this.userService.remove(user, dto);
    }
}
